extends Node


@export var mob_sence:PackedScene
var score

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pass


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass


func game_over() -> void:
	$ScoreTimer.stop()
	$MobTimer.stop()
	$HUD.show_game_over()
	$Music.stop()
	$DeathSound.play()

func new_game() -> void:
	score = 0
	$StartTimer.start()
	$Player.start($StartPosition.position)
	$HUD.update_score(score)
	$HUD.showText("Get Ready")
	get_tree().call_group("mobs","queue_free")
	$Music.play()


func _on_mob_timer_timeout() -> void:
	var mob = mob_sence.instantiate()
	var mobSpawnLocation = $MobPath/MobSpawnLocation
	mobSpawnLocation.progress_ratio = randf()
	
	mob.position = mobSpawnLocation.position
	
	var direction = mobSpawnLocation.rotation + PI/2
	
	direction += randf_range(-PI/4,PI/4)
	mob.rotation = direction
	var velocity = Vector2(randf_range(150,250),0)
	mob.linear_velocity = velocity.rotated(direction)
	add_child(mob)

func _on_score_timer_timeout() -> void:
	score += 1
	$HUD.update_score(score)


func _on_start_timer_timeout() -> void:
	$MobTimer.start()
	$ScoreTimer.start()
